package com.symaster.backups.frame.trayicon;

import com.symaster.backups.frame.MainFrame;
import com.symaster.backups.frame.dialog.PopupMenuDialog;
import com.symaster.common.swing.util.SwingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author yinmiao
 * @version 2024/2/6
 */
@Component
public class ApplicationTrayIcon extends TrayIcon {
    private static final Dimension trayIconSize = SystemTray.getSystemTray().getTrayIconSize();
    private static final Image image = SwingUtil.loadIcon("folder.png", trayIconSize.width, trayIconSize.height).getImage();
    private final MainFrame mainFrame;
    private final PopupMenuDialog popupMenuDialog;

    @Autowired
    public ApplicationTrayIcon(MainFrame mainFrame, PopupMenuDialog popupMenuDialog) {
        super(image, "文件自动备份系统");
        this.mainFrame = mainFrame;
        this.popupMenuDialog = popupMenuDialog;
        generatePopupMenu();
    }

    private void generatePopupMenu() {
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 3) {
                    rightClick(e.getLocationOnScreen());
                }
            }
        });

        this.addActionListener(e -> doubleClick());
    }

    private void rightClick(Point locationOnScreen) {
        popupMenuDialog.open(locationOnScreen);
    }

    private void doubleClick() {
        mainFrame.open();
    }
}
